package com.tonglin.springboot.service;


/**
 *
 *
 * 先进行用户和密码判空
 *
 *
 *
 */

import com.tonglin.springboot.mapper.*;
import com.tonglin.springboot.po.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminService {

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    BookMapper bookMapper;

    @Autowired
    MsgMapper msgMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    BookOrderMapper bookOrderMapper;

    Status status = new Status();
    public Status adminLogin(String name,String psw){



        //1.判断用户名或者密码是否为空

        if(StringUtils.isBlank(name)||StringUtils.isBlank(psw)){
            status.setCode(false);
            status.setMsg("管理员名或者密码不能为空！");
            return status;
        }

        Admin admin = adminMapper.queryAdminByname(name);

        if(admin==null){

            status.setCode(false);
            status.setMsg("管理员名不存在！请重新输入！");
            return status;
        }

        if(!psw.equals(admin.getAdminPsw())){
            status.setCode(false);
            status.setMsg("管理员密码错误！");
            return status;
        }

        status.setCode(true);
        status.setMsg("管理员登录成功！");

        return status;
    }

    //图书下架
    /**
     *
     *
     * 从controller层接受bookName
     *
     * 有一个问题就是：
     *    如果图书下架了，那么订单
     *
     */

    public Status xiaJiaBook(String bookName){

        Status status = new Status();
        Book book = new Book();
        book.setBookName(bookName);

        Book book1 = bookMapper.queryBookByname(bookName);


        if(book1.getBookNumber()<50){
            status.setCode(false);
            status.setMsg("书没有还完！不能下架！");
            return status;
        }

        int temp = bookMapper.delete(book);

        if(temp<1){

            status.setCode(false);
            status.setMsg("图书下架失败");

            return status;
        }

        status.setMsg("下架成功！");
        status.setCode(true);

        return status;

    }
    //图书上架
    /**
     *
     * 接受上架图书的信息
     *
     * 1.对书名、id等等进行判空
     * 2.判断数据库是否有该书
     *     1.id有无
     *     2.书名有无
     * 3.信息满足条件就插入
     *
     */

    public Status shangJiaBook(String bookName,Integer bookId,String bookDes,Integer bookNumber ){

       Status status = new Status();
        //对信息进行判空
        if(StringUtils.isBlank(bookName)
                ||StringUtils.isBlank(bookId.toString())
                ||StringUtils.isBlank(bookDes)||StringUtils.isBlank(bookNumber.toString())){

            status.setMsg("书籍信息填完整！");
            status.setCode(false);

            return  status;
        }

        //判断书的id和书名是否存在书中
        List<String> list = bookMapper.selectName();
        List<Integer> list1 = bookMapper.selectId();

        if(list.contains(bookName)){

            status.setCode(false);
            status.setMsg("该书已经存在！");
            return status;
        }


        if (list1.contains(bookId)){

            status.setCode(false);
            status.setMsg("书籍id重复了，重新填写！");
            return status;
        }

        Book book = new Book();
        book.setBookName(bookName);
        book.setBookId(bookId);
        book.setBookNumber(bookNumber);
        book.setBookDes(bookDes);

          int temp = bookMapper.insert(book);

          if (temp<1){

             status.setCode(false);
             status.setMsg("上架失败！");
             return status;

          }


          status.setMsg("上架成功！");
          status.setCode(true);

          return status;


    }

/**
 *
 * 留言展示
 *
 * 把留言的用户名和留言信息都展示在页面上面
 *
 *
 *
 */

   public  List<Msg> showMsg(){

       //从数据库中查询一条留言记录

     List<Msg>  list = msgMapper.select();


     return list;

   }

    /**
     *
     * 删除用户所有信息
     *
     * 1.如果订单有该用户姓名，就返回不能删除
     * 2.如果没有，则删除,留言的信息也要删除！
     *
     *
     *
     */

    public String deleteUser(String userName){
        String msg;

        User user = new User();
        user.setUserName(userName);

       List<String> list =  bookOrderMapper.queryBookOrderByUsername(userName);

        System.out.println("测试！");
        System.out.println(list.isEmpty());
        for (String li : list
             ) {
            System.out.println("删除用户！");
            System.out.println(li);
        }
       //订单表中没有userName则表明该用户书已经还完了
       if(!list.isEmpty()){
           msg = "用户书还没有还完！不能删除！";

           return msg;
       }

        //1.先删除留言表中该用户的留言信息
        //2.再删除该用户信息

        Msg msg1 = new Msg();
        msg1.setUserName(userName);
        msgMapper.delete(msg1);

        int temp = userMapper.delete(user);

        if(temp<1){

            msg = "删除失败";

            return msg;
        }

        msg = "删除成功！";

        return msg;

    }

    //展示所有用户信息

    public List<User> showUser(){


       List<User> users = userMapper.selectAll();


       return users;


    }

    //将订单信息返回到页面

    public List<BookOrder> showOrder(){


        List<BookOrder> list = bookOrderMapper.selectAll();


        return list;

    }


}
